﻿Public Class FCalculadora
    Private concatenarNumeros As Boolean
    Private primeraOperacion As Boolean
    Private memoria As Object

    Public Sub New()
        ' Llamada necesaria para el diseñador.
        InitializeComponent()
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        concatenarNumeros = False
        primeraOperacion = False
    End Sub

    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMISalir.Click
        Me.Close()
    End Sub

    Private Sub EscribirDisplay(ByVal numero As Integer)
        If (Not (concatenarNumeros)) Then
            Me.TBDisplay.Text = CStr(numero)
            concatenarNumeros = True
        Else
            Me.TBDisplay.Text = Me.TBDisplay.Text + CStr(numero)
        End If
    End Sub

    Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click
        EscribirDisplay(1)
    End Sub

    Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click
        EscribirDisplay(2)
    End Sub

    Private Sub B3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B3.Click
        EscribirDisplay(3)
    End Sub

    Private Sub B4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B4.Click
        EscribirDisplay(4)
    End Sub

    Private Sub B5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B5.Click
        EscribirDisplay(5)
    End Sub

    Private Sub B6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B6.Click
        EscribirDisplay(6)
    End Sub

    Private Sub B7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B7.Click
        EscribirDisplay(7)
    End Sub

    Private Sub B8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B8.Click
        EscribirDisplay(8)
    End Sub

    Private Sub B9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B9.Click
        EscribirDisplay(9)
    End Sub

    Private Sub B0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B0.Click
        If (Not (concatenarNumeros)) Then
            Me.TBDisplay.Text = CStr(0)
        Else
            Me.TBDisplay.Text = Me.TBDisplay.Text + CStr(0)
        End If
    End Sub

    Private Sub TSMIModoEnteros_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIModoEnteros.Click
        Me.BPunto.Enabled = False
        Me.BDivision.Text = "÷"
        Me.TSMIModoComplejo.Checked = False
        Me.TSMIModoReales.Checked = False
        Me.BUndadImaginaria.Enabled = False
        Me.BRcuad.Enabled = False
    End Sub

    Private Sub MSMIModoReales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIModoReales.Click
        Me.BPunto.Enabled = True
        Me.BModulo.Enabled = False
        Me.BDivision.Text = "/"
        Me.TSMIModoComplejo.Checked = False
        Me.TSMIModoEnteros.Checked = False
        Me.BUndadImaginaria.Enabled = False
    End Sub

    Private Sub FCalculadora_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TBDisplay.Focus()
        BPunto.Enabled = False
    End Sub

    Private Sub BCambioSigno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCambioSigno.Click
        Dim temp As Double = Val(TBDisplay.Text)
        temp = -1 * temp
        TBDisplay.Text = CStr(temp)
    End Sub

    Private Sub BSuma_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSuma.Click
        If (Not (primeraOperacion)) Then
            primeraOperacion = True
            If (TSMIModoEnteros.Checked) Then
                memoria = CInt(TBDisplay.Text)
            ElseIf (TSMIModoReales.Checked) Then
                memoria = CDbl(TBDisplay.Text)
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error inesperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        Else
            If (TSMIModoEnteros.Checked) Then
                Dim temp As New COperacionesEnteros
                memoria = temp.sumar(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoReales.Checked) Then
                Dim temp As New COperacionesReales
                memoria = temp.sumar(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error no esperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        End If
        TBDisplay.Text = CStr(memoria)
        concatenarNumeros = False
    End Sub

    Private Sub BResta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BResta.Click
        If (Not (primeraOperacion)) Then
            primeraOperacion = True
            If (TSMIModoEnteros.Checked) Then
                memoria = CInt(TBDisplay.Text)
            ElseIf (TSMIModoReales.Checked) Then
                memoria = CDbl(TBDisplay.Text)
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error inesperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        Else
            If (TSMIModoEnteros.Checked) Then
                Dim temp As New COperacionesEnteros
                memoria = temp.restar(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoReales.Checked) Then
                Dim temp As New COperacionesReales
                memoria = temp.restar(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error no esperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        End If
        TBDisplay.Text = CStr(memoria)
        concatenarNumeros = False
    End Sub

    Private Sub BProducto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BProducto.Click
        If (Not (primeraOperacion)) Then
            primeraOperacion = True
            If (TSMIModoEnteros.Checked) Then
                memoria = CInt(TBDisplay.Text)
            ElseIf (TSMIModoReales.Checked) Then
                memoria = CDbl(TBDisplay.Text)
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error inesperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        Else
            If (TSMIModoEnteros.Checked) Then
                Dim temp As New COperacionesEnteros
                memoria = temp.multiplicar(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoReales.Checked) Then
                Dim temp As New COperacionesReales
                memoria = temp.multiplicar(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error no esperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        End If
        TBDisplay.Text = CStr(memoria)
        concatenarNumeros = False
    End Sub

    Private Sub BDivision_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDivision.Click
        If (Not (primeraOperacion)) Then
            primeraOperacion = True
            If (TSMIModoEnteros.Checked) Then
                memoria = CInt(TBDisplay.Text)
            ElseIf (TSMIModoReales.Checked) Then
                memoria = CDbl(TBDisplay.Text)
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error inesperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        Else
            If (TSMIModoEnteros.Checked) Then
                Dim temp As New COperacionesEnteros
                memoria = temp.dividir(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoReales.Checked) Then
                Dim temp As New COperacionesReales
                memoria = temp.dividir(memoria, Val(TBDisplay.Text))
            ElseIf (TSMIModoComplejo.Checked) Then
                Throw New Exception("Sin implementar aún")
            Else
                MsgBox("Error no esperado", MsgBoxStyle.Critical, "Error inesperado")
                Me.Close()
            End If
        End If
        TBDisplay.Text = CStr(memoria)
        concatenarNumeros = False
    End Sub

    Private Sub BClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BClearAll.Click
        TBDisplay.Text = 0
        memoria = 0
        concatenarNumeros = False
        primeraOperacion = False
    End Sub

    Private Sub BClearError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BClearError.Click
        concatenarNumeros = False
        TBDisplay.Text = 0
    End Sub

    Private Sub TSMIModoComplejo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIModoComplejo.Click
        Me.TSMIModoReales.Checked = False
        Me.TSMIModoEnteros.Checked = False
        BUndadImaginaria.Enabled = True
    End Sub
End Class